var express = require('express');
var router = express.Router();
// 数据库引入
var mongoose = require('mongoose');
var recommend = require('../models/recommend');
var movie = require('../models/movie');
var article = require('../models/article');
var user = require('../models/user');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/mongooseTest', function (req, res, next) {
    mongoose.connect('mongodb://localhost/pets', { useNewUrlParser: true });
    mongoose.Promise = global.Promise;
    var Cat = mongoose.model('Cat', { name: String });
    var tom = new Cat({ name: 'Tom' });
    tom.save( (err) => {
        if (err){
            console.log(err);
        }else{
            console.log('success insert')
        }
    });
    res.send('数据库连接测试')
});

router.get('/addMovieTest', function (req, res, next) {
    mongoose.connect('mongodb://localhost/pets', { useNewUrlParser: true });
    mongoose.Promise = global.Promise;
    var movie = require('../models/movie');
    var movie_1 = new movie({ movieName: 'Tom', movieImg: "111", movieVideo: "222",
        movieTime: "", movieNumSuppose: 0, movieNumDownload: 0, movieMainPage:0 });
    movie_1.save( (err) => {
        if (err){
            console.log(err);
        }else{
            console.log('success insert')
        }
    });
    res.send('数据创建成功')
});

// 显示主页的推荐大图等
router.get('/showIndex', function(req, res, next) {
    recommend.findAll( (err, getRecommend) => {
        res.json({status: 0, message: "获取推荐", data: getRecommend});
    })
});

// 显示所有的排行榜
router.get('/showRanking', function(req, res, next) {
    movie.find({movieMainPage: true}, (err, getMovies) => {
        res.json({status: 0, message: "获取主页", data:getMovies});
    })
});

// 显示文件列表
router.get('/showArticle', function(req, res, next) {
    article.findAll( (err, getArticles) => {
        res.json({status: 0, message: "获取主页", data: getArticles});
    })
});

// 显示文章内容
router.post('/articleDetail', function(req, res, next) {
    if (!req.body.article_id) {
        res.status(400);
        res.json({status: 1, message: "文章id出错"});
    }
    article.findByArticleId(article_id, (err, getArticle) =>{
        res.json({status: 0, message: "获取成功", data: getArticle});
    })
});

// 显示用户个人信息
router.post('/showUser', function(req, res, next) {
    if (!req.body.user_id) {
        res.status(400);
        res.json({status: 1, message: "用户状态出错"});
    }
    user.findById(req.body.user_id, (err, getUser)=> {
        res.json({status: 0, message: "获取成功", data: {
            user_id: getUser._id,
            username: getUser.username,
            userMail: getUser.userMail,
            userPhone: getUser.userPhone,
            userStop: getUser.userStop
        }});
    })
});

module.exports = router;
